package com.link.base.base.delivery.service;


import com.link.base.base.delivery.dao.mybatis.mapper.DeliveryDetailsMapper;
import com.link.base.base.delivery.model.DeliveryDetails;
import com.link.base.base.order.model.SaleOrderItem;
import com.link.base.base.order.service.SaleOrderService;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.query.Filter;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.QueryParamUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author 黄龙建
 * @version 1.0
 * @date: 2018-06-19
 */

@Service
public class DeliveryDetailsServiceImpl extends BasicServiceImpl<DeliveryDetails> implements DeliveryDetailsService {
    @Resource
    private SaleOrderService saleOrderService;
    @Resource
    private DeliveryDetailsMapper deliveryDetailsMapper;

    @Override
    public BasicMapper<DeliveryDetails> getBasicMapper() {
        return deliveryDetailsMapper;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void arrivedOrderItem(List<DeliveryDetails> deliveryItemList) throws Exception {
        List<DeliveryDetails> updateDeliveryList = handleDetailsItemList(deliveryItemList);
        List<SaleOrderItem> updateOrderItemList = new ArrayList<>(updateDeliveryList.size());
        for (DeliveryDetails item : updateDeliveryList) {
            update(item);
            // 组装更新订单行到货数量的数据
            SaleOrderItem saleOrderItem = new SaleOrderItem();
            saleOrderItem.setId(item.getOrderItemId());
            saleOrderItem.setThisQty(item.getThisQty());
            updateOrderItemList.add(saleOrderItem);
        }
        // 更新订单行的到货数量
        String invtxnType = "purchasein";
        saleOrderService.arrivedOrderItem(updateOrderItemList, invtxnType);
    }

    /**
     * 处理传入参数<br/>
     * 1.获得原始发货明细数据<br/>
     * 2.本次到货逻辑计算，本次到货加以到货不能大于发货数量
     *
     * @author NingLanhao
     * @param paramList
     * @return java.util.List
     * @date 2019-01-29 16:31
     */
    private List<DeliveryDetails> handleDetailsItemList(List<DeliveryDetails> paramList) throws Exception {
        if (CollectionUtils.isEmpty(paramList)) {
            throw new ServiceException("DELIVERY-001");
        }
        DeliveryDetails oneDetail = queryById(paramList.get(0).getId());
        Long orderId = oneDetail.getOrderId();
        DeliveryDetails deliveryQuery = (DeliveryDetails) QueryParamUtil.addQpsFilter(DeliveryDetails.class,
                new Filter("orderId", String.valueOf(orderId)));
        List<DeliveryDetails> detailsList = queryByExamplePage(deliveryQuery);

        ArrayList<DeliveryDetails> updateList = new ArrayList<>(detailsList.size());
        for (DeliveryDetails item : paramList) {
            Double qty = item.getThisQty();
            if (qty == null || qty <= 0) {
                // 过滤未填写本次到货数量的部分
                continue;
            }
            for (DeliveryDetails orignal : detailsList) {
                if (!orignal.getId().equals(item.getId())) {
                    continue;
                }
                // 到货总数逻辑合理性判断，已到货 + 本次到货不允许大于发货数
                Double arrivalQty = orignal.getArrivalQty();
                arrivalQty = arrivalQty == null ? 0d : arrivalQty;
                Double thisQty = item.getThisQty();
                double newArrivalQty = arrivalQty + thisQty;
                if (newArrivalQty > orignal.getQty()) {
                    throw new ServiceException("DELIVERY-002");
                }
                orignal.setThisQty(thisQty);
                orignal.setArrivalQty(newArrivalQty);
                updateList.add(orignal);
            }
        }
        return updateList;
    }
}
